<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
  <!-- <script type="text/javascript" src="layui/js/jquery-1.8.3.min.js"></script> -->
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
 
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
 <div class="layui-btn-container">
   <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
   <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
 	<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
 </div>
 
</script>

	
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="returnCar" id="compBtn">赔付</a>
</script>
              
          
<script src="../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 --> 
 

 
<script>
layui.use('table', function(){
  var table = layui.table,
	$ = layui.jquery;
	let value = localStorage.getItem("user");
	var user = JSON.parse(value)

  
  //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
  //详见：https://www.layui.com/doc/modules/table.html#totalRow
  table.render({
	  //绑定哪个组件，通过id选择器,把数据返回给elem绑定id的组件
    elem: '#test'
	//请求数据路径
    ,url:'http://localhost:8085/compInformation/allCompInfo?userId='+user.userId	//绑定三个事件
    ,toolbar: '#toolbarDemo'
	//导出Excel的标题
    ,title: '个人租车情况表'
	//统计总和
    ,totalRow: true
    ,cols: [[
      {type: 'checkbox'}
      ,{field:'orderSerial', title:'订单编号', width:250, unresize: true, sort: true}
      ,{field:'compPrice', title:'赔付金额', width:216}
	  //totalRow: true 开启当前列总和
      ,{field:'compDesc', title:'赔付描述', width:320,sort:true}
      ,{field:'state', title:'赔付状态', width:120}
      // ,{field:'createTime', title:'创建时间', width:180}
	   ,{title:'操作', toolbar: '#barDemo', width:205 }
	 
    ]]
    ,page: true
  });
  
 
  
  //工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        layer.alert(JSON.stringify(data));
      break;
      case 'getCheckLength':
        var data = checkStatus.data;
        layer.msg('选中了：'+ data.length + ' 个');
      break;
      case 'isAll':
        layer.msg(checkStatus.isAll ? '全选': '未全选')
      break;
    };
  });
  
  
  //监听工具条
    table.on('tool(test)', function(obj){
	 
      var data = obj.data;
	  // var info = {orderSerial: data.orderSerial,userId:data.userId}
      if(obj.event === 'returnCar'){
		layer.confirm('确定执行理赔吗?', function(index){
			$.ajax({
				url: 'http://localhost:8085/compInformation/compensate',
				type: 'POST',
				data: {
					orderSerial:data.orderSerial,
					userId:user.userId
				},
				dataType:"json",
				timeout:300000,
				// contentType: 'application/json; charset=UTF-8',
				success:function(result) {
						if(result) {
							layer.msg(result.msg,{time: 1000},function(){
								table.reload('test',table);
								// $('#compBtn').Attr("disabled",true)
							});
						}
					
				}
			});
		});
      }
    });
});
</script>

<style>

 .searchInput{
	 display: inline-block;
 }

.layui-table-cell{
            text-align:center;
            height:auto;
            white-space:normal;
        }
</style>
</body>
</html>
